package database.hibernate;


import java.net.URL;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import database.bean.AuthCardData;
import database.bean.User;

/**
 * Wrapper pour démarrer Hibernate dans un bloc statique d'initialisation.
 * @author Scub-Foundation
 */
public class HibernateUtil {

	private static SessionFactory sessionFactory;
	private static ServiceRegistry serviceRegistry;
	private static URL path;

	static {
		try {
			//chargement du path du fichier xml : normalement compatible pour tous
			path = HibernateUtil.class.getResource("../../hibernate_standalone.cfg.xml");
			//path = HibernateUtil.class.getResource("../../hibernate_standalone_mysql.cfg.xml");
			//System.out.println(path);
			
			// On créé une factory de type Annotation afin de les prendre en charge
			Configuration configuration = new Configuration();
			configuration.configure(path);
			serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		}  catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Accesseur.
	 * @return la session existante
	 * @throws HibernateException
	 */
	public static Session getSession() throws HibernateException {
		return sessionFactory.openSession();
	}

	/**
	 * Ferme la session Hibernate.
	 * @throws HibernateException
	 */
	public static void closeSession() throws HibernateException {
		sessionFactory.close();
	}
	
	/**
	 * Test si un champ existe déjà dans la base de données.
	 * @param session
	 * @param className
	 * @param field
	 * @param value
	 * @return boolean
	 * @throws HibernateException
	 */
	public static Boolean isFieldAlreadyExists(Session session, String className, String field, Object value) throws HibernateException  {
		try {
			@SuppressWarnings("unchecked")
			List<User> resultats = session.createCriteria(Class.forName(className)).add(Restrictions.eq(field, (Object)value)).list();
			if(resultats.size() > 0)	return true;
			else	return false;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return true;
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		try {
			Session session = HibernateUtil.getSession();
			List<User> list1 = session.createCriteria(User.class).list();
			System.out.println("\tUser");
			for(User c : list1)
				System.out.println(c);
			
			List<AuthCardData> list2 = session.createCriteria(AuthCardData.class).list();
			System.out.println("\tCard");
			for(AuthCardData c : list2)
				System.out.println(c);
			
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
